package gui.customizer.panes.logs;

import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Filters extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7483199757994696635L;
	private JTextField txtPrefix;
	private JTextField txtInInterface;
	private JTextField txtOutInterface;
	private JTextField txtSourceAddress;
	private JTextField txtDestinationAddress;
	private JTextField txtMaxLines;
	
	public Filters() {
		setLayout(new MigLayout("", "[grow]15[grow]15[grow]", "[][]20[][]20[][]"));
		
		JLabel lblPrefix = new JLabel("Prefix");
		add(lblPrefix, "cell 0 0");
		
		JLabel lblInInterface = new JLabel("IN Interface");
		add(lblInInterface, "cell 1 0");
		
		JLabel lblOutInterface = new JLabel("OUT Interface");
		add(lblOutInterface, "cell 2 0");
		
		txtPrefix = new JTextField();
		add(txtPrefix, "cell 0 1,growx");
		txtPrefix.setColumns(10);
		
		txtInInterface = new JTextField();
		add(txtInInterface, "cell 1 1,growx");
		txtInInterface.setColumns(10);
		
		txtOutInterface = new JTextField();
		add(txtOutInterface, "cell 2 1,growx");
		txtOutInterface.setColumns(10);
		
		JLabel lblSourceAddress = new JLabel("Source Address");
		add(lblSourceAddress, "cell 0 2");
		
		JLabel lblDestinationAddress = new JLabel("Destination Address");
		add(lblDestinationAddress, "cell 1 2");
		
		txtSourceAddress = new JTextField();
		add(txtSourceAddress, "cell 0 3,growx");
		txtSourceAddress.setColumns(10);
		
		txtDestinationAddress = new JTextField();
		add(txtDestinationAddress, "cell 1 3,growx");
		txtDestinationAddress.setColumns(10);
		
		JLabel lblMaximumLines = new JLabel("Maximum Lines");
		add(lblMaximumLines, "cell 0 4");
		
		txtMaxLines = new JTextField();
		add(txtMaxLines, "cell 0 5,growx");
		txtMaxLines.setColumns(10);
		txtMaxLines.setText("100");
	}
	
	public String getFilterString() {
		StringBuffer buffer = new StringBuffer();
		// Prefix
		if (!txtPrefix.getText().equals(""))
			buffer.append(appendFilter(txtPrefix.getText()));
		// IN Iface
		if (!txtInInterface.getText().equals(""))
			buffer.append(appendFilter("IN=" + txtInInterface.getText()));
		// OUT Iface
		if (!txtOutInterface.getText().equals(""))
			buffer.append(appendFilter("OUT=" + txtOutInterface.getText()));
		// Src Address
		if (!txtSourceAddress.getText().equals(""))
			buffer.append(appendFilter("SRC=" + txtSourceAddress.getText()));
		// Dst Address
		if (!txtDestinationAddress.getText().equals(""))
			buffer.append(appendFilter("DST=" + txtDestinationAddress.getText()));
		// Max Lines
		if (!txtMaxLines.getText().equals(""))
			buffer.append("| tail -n " + txtMaxLines.getText());
		return buffer.toString();
	}
	
	private String appendFilter(String value) {
		return "| grep " + value;
	}
}
